package com.bawei.module_detail.viewmodel

import androidx.lifecycle.viewModelScope
import com.bawei.lib_mvi.base.BaseViewModel
import com.bawei.module_detail.intent.VideoDetailIntent
import com.bawei.module_detail.model.repository.VideoDetailRepository
import com.bawei.module_detail.state.VideoDetailState
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.launch

class VideoDetailViewModel: BaseViewModel<VideoDetailIntent>() {
    private val repository: VideoDetailRepository by  lazy { VideoDetailRepository() }
    private var videoDetailRecommendFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _videoDetailRecommendFlow = videoDetailRecommendFlow.asStateFlow()
    private var simpleVideoCommentFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoCommentFlow = simpleVideoCommentFlow.asStateFlow()
    private var simpleVideoDetailOneDeleteFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailOneDeleteFlow = simpleVideoDetailOneDeleteFlow.asStateFlow()
    private var simpleVideoDetailBulletFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailBulletFlow = simpleVideoDetailBulletFlow.asStateFlow()
    private var simpleVideoDetailSendBulletFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailSendBulletFlow = simpleVideoDetailSendBulletFlow.asStateFlow()
    private var simpleVideoDetailComReplyFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailComReplyFlow = simpleVideoDetailComReplyFlow.asStateFlow()
    private var simpleVideoDetailComReplyDeleteFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailComReplyDeleteFlow = simpleVideoDetailComReplyDeleteFlow.asStateFlow()
    private var simpleVideoDetailSendCommentFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailSendCommentFlow = simpleVideoDetailSendCommentFlow.asStateFlow()
    private var simpleVideoDetailLoveFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailLoveFlow = simpleVideoDetailLoveFlow.asStateFlow()
    private var simpleVideoDetailNoLoveFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailNoLoveFlow = simpleVideoDetailNoLoveFlow.asStateFlow()
    private var simpleVideoDetailStarFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailStarFlow = simpleVideoDetailStarFlow.asStateFlow()
    private var simpleVideoDetailNoStarFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailNoStarFlow = simpleVideoDetailNoStarFlow.asStateFlow()
    private var simpleVideoDetailCheckLoveFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailCheckLoveFlow = simpleVideoDetailCheckLoveFlow.asStateFlow()
    private var simpleVideoDetailCheckCollectionFlow = MutableStateFlow<VideoDetailState>(VideoDetailState.Init)
    val _simpleVideoDetailCheckCollectionFlow = simpleVideoDetailCheckCollectionFlow.asStateFlow()
    override fun handleIntent() {
        viewModelScope.launch {
            intents.consumeAsFlow().collect{
                when(it){
                    is VideoDetailIntent.getCommentByUserId -> getVideoDetailCommentData(it)
                    is VideoDetailIntent.getVideoRecommend -> getVideoDetailRecommendData(it)
                    is VideoDetailIntent.deleteComment -> getVideoDetailDeleteCommentData(it)
                    is VideoDetailIntent.getBullet -> getVideoDetailBulletCommentData(it)
                    is VideoDetailIntent.reply -> getVideoDetailReplyCommentData(it)
                    is VideoDetailIntent.replyDelete -> getVideoDetailReplyDeleteCommentData(it)
                    is VideoDetailIntent.sendComment -> getVideoDetailSendCommentData(it)
                    is VideoDetailIntent.agreeVideo -> getVideoDetailLoveData(it)
                    is VideoDetailIntent.cancelAgreeVideo -> getVideoDetailNoLoveData(it)
                    is VideoDetailIntent.cancelCollectVideo -> getVideoDetailNoStarData(it)
                    is VideoDetailIntent.collectVideo -> getVideoDetailStarData(it)
                    is VideoDetailIntent.sendBullet -> getVideoDetailSendBuffetData(it)
                    is VideoDetailIntent.checkAgreeVideo -> getVideoDetailCheckLoveData(it)
                    is VideoDetailIntent.checkCollectVideo -> getVideoDetailCheckCollectionData(it)
                }
            }
        }
    }
    private fun getVideoDetailRecommendData(it: VideoDetailIntent.getVideoRecommend) {
        viewModelScope.launch {
            repository.getVideoRecommend(it.page,it.pageSize)
                .catch { e->
                    videoDetailRecommendFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        videoDetailRecommendFlow.value = VideoDetailState.VideoRecommendSuccess(it.data)
                    }else{
                        videoDetailRecommendFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailCommentData(it: VideoDetailIntent.getCommentByUserId) {
        viewModelScope.launch {
            repository.getCommentByUserId(it.datatype,it.itemid)
                .catch { e->
                    simpleVideoCommentFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoCommentFlow.value = VideoDetailState.VideoCommentSuccess(it.data)
                    }else{
                        simpleVideoCommentFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailSendCommentData(it: VideoDetailIntent.sendComment) {
        viewModelScope.launch {
            repository.sendComment(it.body)
                .catch { e->
                    simpleVideoDetailSendCommentFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailSendCommentFlow.value = VideoDetailState.SendCommentSuccess(it.data)
                    }else{
                        simpleVideoDetailSendCommentFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailDeleteCommentData(it: VideoDetailIntent.deleteComment) {
        viewModelScope.launch {
            repository.deleteComment(it.commentid)
                .catch { e->
                    simpleVideoDetailOneDeleteFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailOneDeleteFlow.value = VideoDetailState.DeleteCommentSuccess(it.data)
                    }else{
                        simpleVideoDetailOneDeleteFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailReplyCommentData(it: VideoDetailIntent.reply) {
        viewModelScope.launch {
            repository.reply(it.replyBody)
                .catch { e->
                    simpleVideoDetailComReplyFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailComReplyFlow.value = VideoDetailState.ReplyCommentSuccess(it.data)
                    }else{
                        simpleVideoDetailComReplyFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailReplyDeleteCommentData(it: VideoDetailIntent.replyDelete) {
        viewModelScope.launch {
            repository.replyDelete(it.id)
                .catch { e->
                    simpleVideoDetailComReplyDeleteFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailComReplyDeleteFlow.value = VideoDetailState.ReplyDeleteSuccess(it.data)
                    }else{
                        simpleVideoDetailComReplyDeleteFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailBulletCommentData(it: VideoDetailIntent.getBullet) {
        viewModelScope.launch {
            repository.getBullet(it.datatype, it.itemid)
                .catch { e->
                    simpleVideoDetailBulletFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        if(it.data.isEmpty()){
                            simpleVideoDetailBulletFlow.value = VideoDetailState.Empty
                        }else{
                            simpleVideoDetailBulletFlow.value = VideoDetailState.GetBulletSuccess(it.data)
                        }
                    }else{
                        simpleVideoDetailBulletFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailLoveData(it: VideoDetailIntent.agreeVideo) {
        viewModelScope.launch {
            repository.agreeVideo(it.body)
                .catch { e->
                    simpleVideoDetailLoveFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailLoveFlow.value = VideoDetailState.AgreeVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailLoveFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailNoLoveData(it: VideoDetailIntent.cancelAgreeVideo) {
        viewModelScope.launch {
            repository.cancelAgreeVideo(it.body)
                .catch { e->
                    simpleVideoDetailNoLoveFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailNoLoveFlow.value = VideoDetailState.CancelAgreeVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailNoLoveFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailCheckLoveData(it: VideoDetailIntent.checkAgreeVideo) {
        viewModelScope.launch {
            repository.checkAgreeVideo(it.sourceflag, it.userId)
                .catch { e->
                    simpleVideoDetailCheckLoveFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailCheckLoveFlow.value = VideoDetailState.CheckAgreeVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailCheckLoveFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailStarData(it: VideoDetailIntent.collectVideo) {
        viewModelScope.launch {
            repository.collectVideo(it.body)
                .catch { e->
                    simpleVideoDetailStarFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailStarFlow.value = VideoDetailState.CollectVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailStarFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailNoStarData(it: VideoDetailIntent.cancelCollectVideo) {
        viewModelScope.launch {
            repository.cancelCollectVideo(it.body)
                .catch { e->
                    simpleVideoDetailNoStarFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailNoStarFlow.value = VideoDetailState.CancelCollectVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailNoStarFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailCheckCollectionData(it: VideoDetailIntent.checkCollectVideo) {
        viewModelScope.launch {
            repository.checkCollectVideo(it.sourceflag, it.userId)
                .catch { e->
                    simpleVideoDetailCheckCollectionFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailCheckCollectionFlow.value = VideoDetailState.CheckCollectVideoSuccess(it.data)
                    }else{
                        simpleVideoDetailCheckCollectionFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }
    private fun getVideoDetailSendBuffetData(it: VideoDetailIntent.sendBullet) {
        viewModelScope.launch {
            repository.sendBullet(it.body)
                .catch { e->
                    simpleVideoDetailSendBulletFlow.value = VideoDetailState.Failed(e.message.toString())
                }.filter {
                    it != null
                }.collect{
                    if(it.code == 200){
                        simpleVideoDetailSendBulletFlow.value = VideoDetailState.SendBulletSuccess(it.data)
                    }else{
                        simpleVideoDetailSendBulletFlow.value = VideoDetailState.Failed(it.msg)
                    }
                }

        }
    }



}